package org.springblade.minio.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mysql.cj.protocol.x.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.minio.utils.MinioUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ZH
 */
@RestController
@RequestMapping("minio")
@AllArgsConstructor
@Api(value = "对象存储管理", tags = "对象存储管理")
public class MinioController extends BladeController {
	private MinioUtils minioUtils;

	/**
	 * 获取文件下载连接
	 */
	@PostMapping("/getDownloadUrl")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "文件相对路径", notes = "文件相对路径")
	public R<String> getDownloadLink(@ApiParam(value = "文件相对路径",required = true) @RequestParam String path) {
		String url = minioUtils.getUrlByObjectName(path);
		return R.data(url);
	}

	/**
	 * 上传文件
	 */
	@PostMapping("/uploadToMinio")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "上传文件", notes = "上传文件")
	public R uploadToMinio(@ApiParam(value = "文件",required = true) @RequestParam MultipartFile multipartFile,
						   @ApiParam(value = "指定上传地址",required = true) @RequestParam String path) {
		try {
			minioUtils.uploadToMinio(multipartFile.getInputStream(),path);
			return R.success("ok");
		}catch (Exception e){
			return R.fail("file upload failed");
		}
	}

	/**
	 * 根据前缀删除文件
	 */
	@PostMapping("/deletedFileByPrefix")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "根据前缀删除文件", notes = "prefix")
	public R deletedFileByPrefix(@ApiParam(value = "文件路径前缀",required = true) @RequestParam String prefix) {
		minioUtils.deleteObject(prefix);
		return R.success("ok");
	}
}
